package com.jyx.designpatterns;
/**
 *  生产者-消费者 
 * author 		jyx
 * email 		jyxsuper@163.com
 * description: Resource
 * Company: 	myzczk
 * datetime: 	2017年6月7日 下午3:42:08
 */

class Resource
{
	private String name;
	private int count = 1;

	public void set(String name){
		this.name = name + count;
		System.out.println(Thread.currentThread().getName()+"---------生产者--------"+name);
		count += 1;
	}

	public void get(){
		System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
	}

}


/* 生产者 */
class Producer implements Runnable
{
	private Resource r = null;
	public Producer(Resource r){
		this.r = r;
	}

	public void run(){
		r.set("烤鸭");
	}
}

/* 消费者 */
class Consumer implements Runnable
{
	private Resource r;
	public Consumer(Resource r){
		this.r = r;
	}

	public void run(){
		r.get();
	}
}

class  ProducerConsumer
{
	public static void main(String[] args) 
	{
		// 创建资源
		Resource r = new Resource();
		//　穿件任务
		Producer p = new Producer(r);
		Consumer c = new Consumer(r);
		// 创建线程
		Thread t1 = new Thread(p);
		Thread t2 = new Thread(c);
		// 启动线程
		t1.start();
		t2.start();


	}
}
